﻿using System;
using System.Threading.Tasks;
using Data.Entities;
using Microsoft.Practices.Unity;
using Repository.Interface;
using Service.Interface;

namespace Service
{
    public class SysRoleService : ISysRoleService
    {
        #region Repository
        [Dependency]
        protected ISysRoleRepository SysRoleRepository { get; set; }
        #endregion

        #region 创建角色
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task CreateAsync(SysRole role)
        {
            await SysRoleRepository.CreateAsync(role);
        } 
        #endregion

        #region 删除角色
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="role">角色</param>
        /// <returns></returns>
        public async Task DeleteAsync(SysRole role)
        {
            await SysRoleRepository.DeleteAsync(role.Id);
        } 
        #endregion

        #region 通过角色主键获取角色
        /// <summary>
        /// 通过角色主键获取角色
        /// </summary>
        /// <param name="roleId">角色主键</param>
        /// <returns></returns>
        public async Task<SysRole> FindByIdAsync(Guid roleId)
        {
            return await SysRoleRepository.FindAsync(roleId);
        } 
        #endregion

        #region 通过角色名称获取角色
        /// <summary>
        /// 通过角色名称获取角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public async Task<SysRole> FindByNameAsync(string roleName)
        {
            return await SysRoleRepository.FirstAsync(c => c.Name == roleName);
        } 
        #endregion

        #region 更新角色
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns></returns>
        public async Task UpdateAsync(SysRole role)
        {
            await SysRoleRepository.UpdateAsync(role);
        }
        #endregion

        public void Dispose()
        {

        }
    }
}